<template>
  <div class="audio-control">
    <button class="control-btn" @click="toggleMute" :title="isMuted ? '取消静音' : '静音'">
      <span class="control-icon">{{ isMuted ? '🔇' : '🔊' }}</span>
    </button>
    
    <div class="volume-control" v-if="showVolumeControl">
      <div class="volume-slider">
        <label>音效音量</label>
        <input 
          type="range" 
          min="0" 
          max="1" 
          step="0.1" 
          :value="soundVolume" 
          @input="setSoundVolume"
          class="volume-range"
        />
      </div>
      <div class="volume-slider">
        <label>音乐音量</label>
        <input 
          type="range" 
          min="0" 
          max="1" 
          step="0.1" 
          :value="musicVolume" 
          @input="setMusicVolume"
          class="volume-range"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import audioManager from '../utils/audioManager.js'

export default {
  name: 'AudioControl',
  setup() {
    const showVolumeControl = ref(false)
    
    const isMuted = computed(() => audioManager.isMuted)
    const soundVolume = computed(() => audioManager.volume)
    const musicVolume = computed(() => audioManager.musicVolume)
    
    const toggleMute = () => {
      audioManager.toggleMute()
    }
    
    const setSoundVolume = (event) => {
      const volume = parseFloat(event.target.value)
      audioManager.setVolume(volume)
    }
    
    const setMusicVolume = (event) => {
      const volume = parseFloat(event.target.value)
      audioManager.setMusicVolume(volume)
    }
    
    const toggleVolumeControl = () => {
      showVolumeControl.value = !showVolumeControl.value
    }
    
    return {
      showVolumeControl,
      isMuted,
      soundVolume,
      musicVolume,
      toggleMute,
      setSoundVolume,
      setMusicVolume,
      toggleVolumeControl
    }
  }
}
</script>

<style scoped>
.audio-control {
  position: fixed;
  top: 20px;
  left: 20px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.control-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(255, 107, 157, 0.3);
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.control-btn:hover {
  background: rgba(255, 107, 157, 0.1);
  border-color: rgba(255, 107, 157, 0.5);
  transform: scale(1.1);
  box-shadow: 0 4px 12px rgba(255, 107, 157, 0.2);
}

.control-icon {
  font-size: 18px;
  transition: transform 0.3s ease;
}

.control-btn:hover .control-icon {
  transform: scale(1.1);
}

.volume-control {
  margin-top: 10px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  padding: 15px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(15px);
  border: 1px solid rgba(255, 107, 157, 0.2);
  min-width: 200px;
}

.volume-slider {
  margin-bottom: 10px;
}

.volume-slider:last-child {
  margin-bottom: 0;
}

.volume-slider label {
  display: block;
  font-size: 0.8rem;
  color: #495057;
  margin-bottom: 5px;
  font-family: 'LoveFont', cursive;
  font-weight: 500;
}

.volume-range {
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: #e9ecef;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

.volume-range::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #ff6b9d;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
}

.volume-range::-webkit-slider-thumb:hover {
  background: #ff4d8d;
  transform: scale(1.2);
  box-shadow: 0 4px 8px rgba(255, 107, 157, 0.3);
}

.volume-range::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #ff6b9d;
  cursor: pointer;
  border: none;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
}

.volume-range::-moz-range-thumb:hover {
  background: #ff4d8d;
  transform: scale(1.2);
  box-shadow: 0 4px 8px rgba(255, 107, 157, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .audio-control {
    top: 10px;
    left: 10px;
  }
  
  .control-btn {
    width: 35px;
    height: 35px;
  }
  
  .control-icon {
    font-size: 16px;
  }
  
  .volume-control {
    min-width: 180px;
    padding: 12px;
  }
  
  .volume-slider label {
    font-size: 0.75rem;
  }
}
</style>
